using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace GarbageTruck.DayOfTheWeek
{
    public class DayOfTheWeek : ITemporalExpression
    {

        #region CONSTRUCTORS

        public DayOfTheWeek()
        {
        }

        public DayOfTheWeek(string expresion)
        {
            this.Day = (DayOfWeek)int.Parse(expresion);
        }

        public DayOfTheWeek(DayOfWeek day)
        {
            this.Day = day;
        }

        #endregion CONSTRUCTORS

        #region FIELDS

        private DayOfWeek m_Day;
        private Calendar calendar = CultureInfo.CurrentCulture.Calendar;

        #endregion FIELDS

        #region PROPERTIES

        public DayOfWeek Day
        {
            get { return m_Day; }
            set { m_Day = value; }
        }

        #endregion PROPERTIES

        #region METHODS

        public bool Contains(DateTime dateTime)
        {
            return (dateTime.DayOfWeek == this.Day);
        }

        public DateTime? GetNextOcurrence(DateTime dateTime)
        {
            int value = ((int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek - (int)dateTime.DayOfWeek);
            DateTime curentWeekOcurrence = dateTime.Date.AddDays(value);
            DateTime nextWeekOcurrence = calendar.AddWeeks(curentWeekOcurrence, 1);
            return ((dateTime.Date < curentWeekOcurrence) ? curentWeekOcurrence : nextWeekOcurrence);
        }

        public DateTime? GetEventEnd(DateTime dateTime)
        {
            return dateTime;
        }

        public override string ToString()
        {
            return string.Format("DOTW({0})", (int)this.Day);
        }

        #endregion METHODS

    }
}
